<?php
namespace app\index\controller;

use app\common\model\index\NavModel;
use app\common\model\index\UserModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\Library;
use think\admin\model\SystemBase;
use think\App;

/**
 * index 基类
 * @package app\index\controller
 * @author wangqinghui
 * @DateTime 2024-09-12 11:20:33
 */
class BaseIndex extends Controller
{

    // 是否登录
    protected $login = false;

    // 用户ID
    protected $user_id = 0;

    // 是否强制登录
    protected $need_login = false;

    // 是否校验登录
    protected $check_login = false;

    // 用户信息
    protected $userInfo = [];

    // 是否需要提示完善信息
    protected $need_complete_user = false;
    /**
     * 初始化
     * @author wangqinghui
     * @DateTime 2024-09-03 18:56:18
     * @return void
     */
    protected function initialize()
    {
        // 获取导航
        $this->getIndexHeader();

        // 校验是否登录
        if($this->check_login){
            $this->isLogin();
        }

        // 强制登录
        if($this->need_login){
            $this->isLogin();
            $this->redirectLogin();
        }

        // 获取底部配置
        $this->getFooterInfo();
    }

    /**
     * 获取公共头部信息
     * @author wangqinghui
     * @DateTime 2024-09-03 17:51:26
     * @return void
     */
    protected function getIndexHeader(): void
    {
        // 获取导航数据
        $map = [
            ['status', '=', 1]// 启用
        ];
        $nav_list = NavModel::mk()->where($map)->order('sort desc,id asc')->select()->toArray();
        if(!empty($nav_list)){
            foreach ($nav_list as $key => $value) {
                $queryParams = [
                    'nav_id' => $value['id']
                ];
                // 构建查询字符串
                $queryStr = http_build_query($queryParams);
                if(strpos($value['link'], '?') !== false){
                    $nav_list[$key]['link'] = $value['link'].'&'.$queryStr;
                }else{
                    $nav_list[$key]['link'] = $value['link'].'?'.$queryStr;
                }
            }
        }
        $this->navs = DataExtend::arr2tree($nav_list);
    }

    /**
     *
     * @author wangqinghui
     * @DateTime 2024-09-03 17:50:59
     * @return void
     */
    protected function isLogin(): void
    {
        $user_id = Library::$sapp->session->get('index_user.id', 0);
        $this->login = ($user_id > 0) ? true : false;
        $this->user_id = $user_id;
        $this->userInfo = Library::$sapp->session->get('index_user', []);
    }

    /**
     * 跳转登录
     * @author wangqinghui
     * @DateTime 2024-09-09 15:24:11
     * @return void
     */
    protected function redirectLogin(): void
    {
        if(!$this->login){
            $this->redirect(sysuri('login/index'));
        }
        $userInfo = UserModel::mk()->where('id', $this->user_id)->find();
        if(empty($userInfo['id'])){
            $this->redirect(sysuri('login/index'));
        }
        if($userInfo['is_deleted'] == 1){
            $this->redirect(sysuri('login/index'));
        }
        $this->userInfo = $userInfo->toArray();
    }

    /**
     * 获取网站底部配置
     * @author wangqinghui
     * @DateTime 2024-09-29 14:13:24
     * @return void
     */
    protected function getFooterInfo()
    {
        $map = [
            ['type',    '=',    '官网配置'],
            ['code',    '=',    'website_footer'],
            ['status',  '=',    1],
            ['deleted',  '=',    0]
        ];
        $power = SystemBase::mk()->where($map)->value('content');
        $this->footer = [
            'copyright' => 'Copyright © '.date('Y'),
            'powered'   => $power ?? '',
        ];
    }

    /**
     * 检测是否需要完善信息
     * @author wangqinghui
     * @DateTime 2024-10-10 10:55:58
     * @return void
     */
    protected function checkCompleteUser()
    {
        $userInfo = UserModel::mk()->where('id', $this->user_id)->find();
        if(empty($userInfo['true_name']) || empty($userInfo['mobile']) || empty($userInfo['job_place']) || !isset($userInfo['edu_background'])
        || empty($userInfo['nick_name']) || empty($userInfo['job_number']) || !isset($userInfo['sex'])){
            $this->need_complete_user = true;
        }
    }
}